package com.yilin.tms.capital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.reflect.TypeToken;
import com.yilin.tms.capital.mapper.BankCardMapper;
import com.yilin.tms.capital.mapper.PayChannelMapper;
import com.yilin.tms.capital.mapper.TradeRecordMapper;
import com.yilin.tms.capital.service.IBankCardService;
import com.yilin.tms.capital.service.ITradeRecordService;
import com.yilin.commons.util.ArrayUtil;
import com.yilin.commons.util.DateTimeUtil;
import com.yilin.commons.util.JsonUtil;
import com.yilin.commons.util.MapUtil;
import com.yilin.tms.core.application.AppUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.capital.BankCard;
import com.yilin.tms.core.entity.capital.PayChannel;
import com.yilin.tms.core.entity.capital.TradeRecord;
import com.yilin.tms.core.entity.order.payment.OrderPaymentCost;
import com.yilin.tms.core.entity.order.payment.OrderSettlementCost;
import com.yilin.tms.core.remote.IOrderRemote;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/capital/migration/")
public class MigrationController extends BaseController {
    @Resource
    TradeRecordMapper tradeRecordMapper;
    @Resource
    BankCardMapper bankCardMapper;
    @Resource
    PayChannelMapper payChannelMapper;

    /**
     * 获取所有交易记录
     * */
    @PostMapping("tradeRecordList")
    @SkipLoginVerify
    public ReturnData<Object> tradeRecordList(int page) {
        PageData<TradeRecord> data = tradeRecordMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<TradeRecord>().orderByAsc("create_time"));
        String toJson = JsonUtil.objectToJson(data.getList());
        List<Map<String, Object>> dataList = JsonUtil.jsonToObject(toJson, new TypeToken<List<Map<String, Object>>>() {});
        for (Map<String, Object> map : dataList) {
            if(map.get("tmsCapitalDataState")!=null){
                if(map.get("tmsCapitalDataState").equals("error")){
                    map.put("tmsCapitalDataState","wait");
                }
                if(map.get("tmsCapitalDataState").equals("creating")){
                    map.put("tmsCapitalDataState","wait");
                }
                if(map.get("tmsCapitalDataState").equals("success")){
                    map.put("tmsCapitalDataState","complete");
                }
            }
            if(map.get("tradeType")!=null){
                if(map.get("tradeType").equals("payment")){
                    map.put("tradeType","paymentCost");
                }
                if(map.get("tradeType").equals("settlement")){
                    map.put("tradeType","settlementCost");
                }
                if(map.get("tradeType").equals("service")){
                    map.put("tradeType","serviceCost");
                }
                if(map.get("tradeType").equals("refund")){
                    map.put("tradeType","refundCost");
                }
            }
        }
        return jsonView(SUCCESS_CODE, "移植完成",dataList);
    }

    /**
     * 获取所有支付通道
     * */
    @PostMapping("payChannelList")
    @SkipLoginVerify
    public ReturnData<Object> payChannelList(int page) {
        PageData<PayChannel> data = payChannelMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<PayChannel>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 获取所有银行卡
     * */
    @PostMapping("bankCardList")
    @SkipLoginVerify
    public ReturnData<Object> bankCardList(int page) {
        PageData<BankCard> data = bankCardMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<BankCard>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

}
